Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
@datastructures-js/binary-search-tree
Advanced tools
binary search tree & avl tree (self balancing tree) implementation in javascript
Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript.
Binary Search Tree | |
AVL Tree (Self Balancing Tree) |
npm install --save @datastructures-js/binary-search-tree
Both trees have the same interface except that AVL tree will maintain itself balanced by rotating the nodes that become unbalanced during insertion and deletion. If your code requires a strictly balanced tree that always benefits from the log(n) runtime of insert & remove, you should use the AVL one.
const { BinarySearchTree, AvlTree } = require('@datastructures-js/binary-search-tree');
import { BinarySearchTree, AvlTree } from '@datastructures-js/binary-search-tree';
const bst = new BinarySearchTree();
// OR a self balancing tree
const bst = new AvlTree();
inserts a node with key/value into the tree. Inserting an node with existing key, would update the existing node's value with the new one. AVL tree will rotate nodes properly if the tree becomes unbalanced during insertion.
params | |
---|---|
name | type |
key | number or string |
value | object |
return | |
---|---|
BinarySearchTree | BinarySearchTreeNode |
AvlTree | AvlTreeNode |
runtime |
---|
O(log(n)) |
bst.insert(50, 'v1');
bst.insert(80, 'v2');
bst.insert(30, 'v3');
bst.insert(90, 'v4');
bst.insert(60, 'v5');
bst.insert(40, 'v6');
bst.insert(20, 'v7');
checks if a node exists by its key.
params | |
---|---|
name | type |
key | number or string |
return |
---|
boolean |
runtime |
---|
O(log(n)) |
bst.has(50); // true
bst.has(100); // false
finds a node in the tree by its key.
params | |
---|---|
name | type |
key | number or string |
return | |
---|---|
BinarySearchTree | BinarySearchTreeNode |
AvlTree | AvlTreeNode |
runtime |
---|
O(log(n)) |
const n60 = bst.find(60);
console.log(n60.getKey()); // 60
console.log(n60.getValue()); // v5
console.log(bst.find(100)); // null
finds the node with min key in the tree.
return | |
---|---|
BinarySearchTree | BinarySearchTreeNode |
AvlTree | AvlTreeNode |
runtime |
---|
O(log(n)) |
const min = bst.min();
console.log(min.getKey()); // 20
console.log(min.getValue()); // v7
finds the node with max key in the tree.
return | |
---|---|
BinarySearchTree | BinarySearchTreeNode |
AvlTree | AvlTreeNode |
runtime |
---|
O(log(n)) |
const max = bst.max();
console.log(max.getKey()); // 90
console.log(max.getValue()); // v4
returns the root node of the tree.
return | |
---|---|
BinarySearchTree | BinarySearchTreeNode |
AvlTree | AvlTreeNode |
runtime |
---|
O(1) |
const root = bst.root();
console.log(root.getKey()); // 50
console.log(root.getValue()); // v1
returns the count of nodes in the tree.
return |
---|
number |
runtime |
---|
O(1) |
console.log(bst.count()); // 7
traverses the tree in order (left-node-right).
params | ||
---|---|---|
name | type | description |
cb | function | called with each node |
runtime |
---|
O(n) |
bst.traverseInOrder((node) => console.log(node.getKey()));
/*
20
30
40
50
60
80
90
*/
traverses the tree pre order (node-left-right).
params | ||
---|---|---|
name | type | description |
cb | function | called with each node |
runtime |
---|
O(n) |
bst.traversePreOrder((node) => console.log(node.getKey()));
/*
50
30
20
40
80
60
90
*/
traverses the tree post order (left-right-node).
params | ||
---|---|---|
name | type | description |
cb | function | called with each node |
runtime |
---|
O(n) |
bst.traversePostOrder((node) => console.log(node.getKey()));
/*
20
40
30
60
90
80
50
*/
removes a node from the tree by its key. AVL tree will rotate nodes properly if the tree becomes unbalanced during deletion.
params | |
---|---|
name | type |
key | number or string |
return |
---|
boolean |
runtime |
---|
O(log(n)) |
bst.remove(20); // true
bst.remove(100); // false
console.log(bst.count()); // 6
clears the tree.
runtime |
---|
O(1) |
bst.clear();
console.log(bst.count()); // 0
console.log(bst.root()); // null
returns the node's key that is used to compare with other nodes.
return |
---|
number or string |
change the value that is associated with a node.
params | |
---|---|
name | type |
value | object |
returns the value that is associated with a node.
return |
---|
object |
returns node's left child node.
return | |
---|---|
BinarySearchTree | BinarySearchTreeNode |
AvlTree | AvlTreeNode |
returns node's right child node.
return | |
---|---|
BinarySearchTree | BinarySearchTreeNode |
AvlTree | AvlTreeNode |
returns node's parent node.
return | |
---|---|
BinarySearchTree | BinarySearchTreeNode |
AvlTree | AvlTreeNode |
extends BinarySearchTreeNode and adds the following methods:
the height of the node in the tree. root height is 1.
return |
---|
number |
the height of the left child. 0 if no left child.
return |
---|
number |
the height of the right child. 0 if no right child.
return |
---|
number |
returns the node's balance by subtracting right height from left height.
return |
---|
number |
grunt build
The MIT License. Full License is here
FAQs
binary search tree & avl tree (self balancing tree) implementation in javascript
We found that @datastructures-js/binary-search-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.